package com.thinkpadteam.edugra.game;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
 * 
 * @author manish.s
 *
 */

public class SQLiteConnector {
	
	private SQLiteDatabase database;
	private SQLiteHelper sqlHelper;
	private Cursor cursor;
	
	private static final String TABLE_RECORD_MENCOCOKKAN = "game_mencocokkan_gambar";
	private static final String TABLE_RECORD_BERHITUNG = "game_berhitung";
	
	public SQLiteConnector(Context context) {
		sqlHelper = new SQLiteHelper(context);
	}
	
	// Getting All records
			public List getAllRecord(int tab) {
				Soal soal;
				//for debug
				Soal soala = new Soal();
				Soal soalb = new Soal();
				String a ="";
				String b = "";
				//end for debug
				List<Soal> listDetailSoal = new ArrayList<Soal>();
				String selectQuery;
				if(tab==1){
					 selectQuery="SELECT * FROM " + TABLE_RECORD_MENCOCOKKAN + " ORDER BY RANDOM()";					
				}else{
					selectQuery="SELECT * FROM " + TABLE_RECORD_BERHITUNG + " ORDER BY RANDOM()";
				}


				database = sqlHelper.getReadableDatabase();
				cursor = database.rawQuery(selectQuery, null);
				int i=0;
				
//				if (cursor.moveToFirst()) {
				while (cursor.moveToNext()) {
						soal = new Soal();
						soal.setDesc(cursor.getString(1));
						soal.setPath(cursor.getString(2));
						soal.setPilihan1(cursor.getString(3));
						soal.setPilihan2(cursor.getString(4));
						soal.setPilihan3(cursor.getString(5));
						soal.setJawaban(cursor.getString(6));
						soal.setKategori(cursor.getString(7));
						listDetailSoal.add(i, soal);
						//for debug
						Log.d("soal sqlite conector "+i, listDetailSoal.get(i).getPilihan2());
						if(i == 0){
							a = listDetailSoal.get(i).getPilihan2();
							soala = soal;
						}else{
							b = listDetailSoal.get(i).getPilihan2();
							soalb = soal;
						}
						//end for debug
						i++;
						
					} 
//				}
				//for debug
				Log.d("soal sqlite conector a", a);
				Log.d("soal sqlite conector b", b);
				//end for debug
				database.close();
				return listDetailSoal;
			}
			
			
			public List<Soal> getAllRecordKategori(String kategori) {
				Soal soal = new Soal();
				List<Soal> listDetailSoal = new ArrayList<Soal>();
				String selectQuery = "SELECT * FROM " + TABLE_RECORD_BERHITUNG +" WHERE KATEGORI = "+kategori +" ORDER BY RANDOM()";

				database = sqlHelper.getReadableDatabase();
				cursor = database.rawQuery(selectQuery, null);
				if (cursor.moveToFirst()) {
					do {
						soal.setDesc(cursor.getString(1));
						soal.setPath(cursor.getString(2));
						soal.setPilihan1(cursor.getString(3));
						soal.setPilihan2(cursor.getString(4));
						soal.setPilihan3(cursor.getString(5));
						soal.setKategori(cursor.getString(6));
						listDetailSoal.add(soal);
						
					} while (cursor.moveToNext());
				}
				database.close();
				return listDetailSoal;
			}
			
			
	
}
